﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class CreateMember : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            String pUserName = Request.Form["pUserName"];
            String pPassword = Request.Form["pPassword"];
            String pFullName = Request.Form["pFullName"];
            int pRole = Convert.ToInt32(Request.Form["pRole"]);
            if (pUserName != null && pPassword != null && pFullName != null)
            {
                MemberBA memberBA = new MemberBA();
                
                Boolean temp = true;
                String err = "";
                if ("".Equals(pUserName))
                {
                    err += "User name can not be empty<br>";
                    temp = false;
                }
                else 
                {
                    MemberTO memberTO = memberBA.GetMemberByUsername(pUserName);
                    if (memberTO.UserName != null)
                    {
                        err += "User name exists<br>";
                        temp = false;
                    }
                }
                if ("".Equals(pPassword))
                {
                    err += "Password can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pFullName))
                {
                    err += "Full name can not be empty<br>";
                    temp = false;
                }
                
                if (temp)
                {
                    Boolean stt = false;
                    MemberTO member = new MemberTO();
                    member.UserName = pUserName;
                    member.Password = Md5.md5(pPassword);
                    member.FullName = pFullName;
                    member.Role = pRole;
                    member.LastLogOnDate = DateTime.Now;
                    
                    stt = memberBA.InsertMember(member);
                    if (stt == true)
                    {
                        Attributes.Add("success", "Insert success");
                        return;
                    }
                    else
                    {
                        Attributes.Add("err", "Insert faulty");
                        return;
                    }
                }
                else
                {
                    Attributes.Add("err", err);
                }
            }
        }
    }
}